const app = getApp();
Page({
	todaystep: [],
	totalstep: [],
	data: {
		type_1: ' ',
		on_1: 'on',
		on_2: ' ',
		on_3: ' ',
		b_wrds: '今日步数',
		first_wrds: '夺得今日运动冠军',
		top_type: '',
		fan_url:'../index/index'
	},
	onLoad: function(options) {
		var that = this;
    that.setData({
      isGet: wx.getStorageSync('isGetChicken')
    })
		that.hei = wx.getSystemInfoSync().windowHeight;
		that.wid = wx.getSystemInfoSync().windowWidth * 0.15;
		that.list1_num = 35;
		that.list2_num = 35;
		that.list3_num = 35;
		that.scroll_true = '1';
		that.list_1 = 'true';
		that.list_2 = 'true';
		that.list_3 = 'true';
		that.list1_page = 1;
		that.list2_page = 1;
		that.list3_page = 1;
		that.list_1_suo = 'true';
		that.list_2_suo = 'true';
		that.list_3_suo = 'true';
		//判断那里过来的
		console.log(options)
		if(options.fan_url){
			this.setData({
				fan_url:'../' + options.fan_url
			})
		};
    this.setData({
      isGet: wx.getStorageSync('isGetChicken')
    })
		/**
		 * 积分明细接口
		 * type=2 为支出项
		 */
		app.request(app.apiDomain + 'index.php?c=point&a=todaystepranks', {
			page: 1,
			size: 30,
			type: 1
		}, function(res) {
            // console.log(res)
			if (res.data.status == '1') {
				that.todaystep = res.data.data;
        let user = res.data.data.user
        console.log(user)
        if (!!user.hasOwnProperty('total')) {
          if (Number(user.total) > 1000 && Number(user.total) < 10000) {
            user.total = Number(Number(user.total) / 1000).toString().match(/^\d+(?:\.\d{0,2})?/) + 'K'
          } else if (Number(user.total) >= 10000) {
            user.total = Number(Number(user.total) / 10000).toString().match(/^\d+(?:\.\d{0,2})?/) + 'W'
          }
        }
				that.setData({
					step_rank: res.data.data.list,
					top: res.data.data.top,
					user: user
				});
				//点赞
				// console.log(res.data.data.list)

			} else {
				// wx.navigateTo({
				// 	url: '../logs/accredit?jump=../Ranking/list'
				// })
			}
		});
		// 总金币
		app.request(app.apiDomain + 'index.php?c=point&a=totalpointranks', {
			page: 1,
			size: 30,
			type: 1
		}, function(res) {
            // console.log(res)
			if (res.data.status == '1') {
				that.totalpoint = res.data.data;
        // let list = res.data.data.list.forEach(function (item, index) {
        //   if (parseFloat(item.balance_point) > 1000) {
        //     item.balance_point = parseFloat(parseFloat(item.balance_point) / 1000).toFixed(2) + 'K'
        //   } else if (parseFloat(item.balance_point) > 10000) {
        //     item.balance_point = parseFloat(parseFloat(item.balance_point) / 10000).toFixed(2) + 'W'
        //   }
        //   return item
        // })
        let total = res.data.data.total
        if (Number(total) > 1000 && Number(total) < 10000) {
          total = Number(Number(total) / 1000).toString().match(/^\d+(?:\.\d{0,2})?/) + 'K'
        } else if (Number(total) >= 10000) {
          total = Number(Number(total) / 10000).toString().match(/^\d+(?:\.\d{0,2})?/) + 'W'
        }
        // that.totalpoint.total = total
        that.totalpoint.user.total = total
        console.log(that.totalpoint)
				that.setData({
					total_rank: res.data.data.list
				});
			}
		});
        // 早起
        app.request(app.apiDomain + 'index.php?c=point&a=checkinranks', {
			page: 1,
			size: 30,
			type: 1
		}, function(res) {
            // console.log(res)
			if (res.data.status == '1') {
                that.getUpData = res.data.data
                that.setData({
                    getUp_rank : res.data.data.list
                })
			}
		});
	},
	formSubmit: function(e) {
		// console.log(e.detail);
		var ind = e.detail.target.dataset.index;
        var zan_id = e.detail.target.dataset.id;
		var num = parseFloat(e.detail.target.dataset.num) + 1
		if (this.scroll_true == '1') {
			this.setData({
				['step_rank[' + ind + '].liked']: 'true',
				['step_rank[' + ind + '].like_num']: num

			});
			var type = 'day_step'
			// console.log(1)
		} else if(this.scroll_true == '2') {
			// console.log(2)
			this.setData({
				['total_rank[' + ind + '].liked']: 'true',
				['total_rank[' + ind + '].like_num']: num
			});
			var type = 'point_account'
		} else if(this.scroll_true == '3'){
            // console.log(3)
            this.setData({
				['getUp_rank[' + ind + '].liked']: 'true',
				['getUp_rank[' + ind + '].like_num']: num
			});
			var type = 'check_in'
        }
		//发给服务器
		app.request(app.apiDomain + 'index.php?c=user&a=like', {
			// user
			id: zan_id,
			type: type,
			form_id: e.detail.formId,
		}, function(res) {
			// console.log(res.data.info)
		});
		const innerAudioContext = wx.createInnerAudioContext('zan')
		innerAudioContext.autoplay = true
		innerAudioContext.src = 'http://resource.160nurse.com/canvas/healthy/img/zou_lu/zan.mp3'
		innerAudioContext.onPlay(() => {
			console.log('开始播放')
		});
	},
	list_click: function(e) {
		var sid = e.currentTarget.dataset.id;
		var a = this.id_data;
		var index = '';
		if (a.length > 0) {
			index = a.indexOf(sid);
			if (index > -1) {
				a.splice(index, 1);
			} else {
				a.push(sid)
			}
		} else {
			a.push(sid)
		}
		// console.log(a)

		this.setData({
			id: a //把获取的自定义id赋给当前组件的id(即获取当前组件)
		})
	},
	hd_click: function(e) {
        // console.log(e)
		var sid = e.currentTarget.dataset.id;
		if (sid == '1') {
			this.setData({
				type_1: ' ',
				on_1: 'on',
				on_2: ' ',
				on_3: ' ',
				b_wrds: '今日步数',
				first_wrds: '夺得今日运动冠军',
				user: this.todaystep.user,
				top: this.todaystep.top,
				top_type: ''
			})
			this.scroll_true = '1';
        } else if(sid == '2') {
			this.setData({
				type_1: 'type_1',
				on_1: ' ',
				on_2: 'on',
				on_3: '',
				b_wrds: '总健康币',
				first_wrds: '夺得今日健康首富',
				user: this.totalpoint.user,
				top: this.totalpoint.top,
				top_type: 'type_2'
			});
			this.scroll_true = '2';
		}else if(sid == '3'){
            this.setData({
				type_1: 'type_1',
				on_1: ' ',
				on_2: ' ',
				on_3: 'on',
				b_wrds: '早起次数',
				first_wrds: '夺得今日早起冠军',
				user: this.getUpData.user,
				top: this.getUpData.top,
				top_type: 'type_3'
            });
            this.scroll_true = '3';
        }
	},
	onShareAppMessage: function() {
		return app.onShareAppMessage();
	},
	onPageScroll: function(e) {
		// 		console.log(e.scrollTop+this.hei)
		var Page_this = this;
		if (this.scroll_true == '1') { //判断是list1滚动还是2在滚动
			if (this.list_1_suo == 'false') {
				return false
			};
			// console.log(this.list_1)
			if (this.list_1 == 'false') { //判断是否已经没有数据
				wx.showToast({
					title: '已经到底了',
					icon: 'success',
					duration: 1000
				})
				this.list_1_suo = 'false' //锁定加载
				return false
			};
			if (e.scrollTop + this.hei > (this.wid * this.list1_num)) { //判断是否已经到了加载的位置
				wx.showLoading({
					title: '加载中',
				});
				this.list1_num = this.list1_num + 30; //修改下一次加载位置
				this.list1_page++; //list1的页数
				this.list_1_suo = 'false' //锁定加载
				app.request(app.apiDomain + 'index.php?c=point&a=todaystepranks', {
					page: this.list1_page,
					size: 30,
					type: 1
				}, function(res) {
					if (res.data.status == '1') {
						Page_this.setData({
							step_rank: Page_this.data.step_rank.concat(res.data.data.list),
						});
						setTimeout(function() {
							wx.hideLoading() //加载完成关闭窗口
							Page_this.list_1_suo = 'true' //解除锁定
						}, 500);
						if (res.data.data.list.length < 30) {
							// console.log(1)
							Page_this.list_1 = 'false';
						}
					} else {
						wx.showToast({
							title: '加载失败',
							icon: 'success',
							duration: 2000
						})
					};
				});
			}
		} else if(this.scroll_true == '2') {
			if (this.list_2_suo == 'false') {
				return false
			};
			if (this.list_2 == 'false') { //判断是否已经没有数据
				wx.showToast({
					title: '已经到底了',
					icon: 'success',
					duration: 1000
				})
				this.list_2_suo = 'false' //锁定加载
				return false
			};
			if (e.scrollTop + this.hei > (this.wid * this.list2_num)) {
				wx.showLoading({
					title: '加载中',
				});
				this.list2_num = this.list2_num + 30; //修改下一次加载位置
				this.list2_page++; //list2的页数
				this.list_2_suo = 'false' //锁定加载
				app.request(app.apiDomain + 'index.php?c=point&a=totalpointranks', {
					page: this.list2_page,
					size: 30,
					type: 1
				}, function(res) {
					if (res.data.status == '1') {
						Page_this.setData({
							total_rank: Page_this.data.total_rank.concat(res.data.data.list),
						});
						setTimeout(function() {
							wx.hideLoading() //加载完成关闭窗口
							Page_this.list_2_suo = 'true' //解除锁定
						}, 500);
						if (res.data.data.list.length < 30) {
							// console.log(2)
							Page_this.list_2 = 'false';
						}
					} else {
						wx.showToast({
							title: '加载失败',
							icon: 'success',
							duration: 2000
						})
					};
				});
			}
		}else if(this.scroll_true == '3'){
            // console.log(333)
            if (this.list_3_suo == 'false') {
				return false
			};
			if (this.list_3 == 'false') { //判断是否已经没有数据
				wx.showToast({
					title: '已经到底了',
					icon: 'success',
					duration: 1000
				})
				this.list_3_suo = 'false' //锁定加载
				return false
			};
			if (e.scrollTop + this.hei > (this.wid * this.list3_num)) {
				wx.showLoading({
					title: '加载中',
				});
				this.list3_num = this.list3_num + 30; //修改下一次加载位置
				this.list3_page++; //list2的页数
				this.list_3_suo = 'false' //锁定加载
				app.request(app.apiDomain + 'index.php?c=point&a=checkinranks', {
					page: this.list3_page,
					size: 30,
					type: 1
				}, function(res) {
					if (res.data.status == '1') {
						Page_this.setData({
							getUp_rank: Page_this.data.getUp_rank.concat(res.data.data.list),
						});
						setTimeout(function() {
							wx.hideLoading() //加载完成关闭窗口
							Page_this.list_3_suo = 'true' //解除锁定
						}, 500);
						if (res.data.data.list.length < 30) {
							// console.log(2)
							Page_this.list_3 = 'false';
						}
					} else {
						wx.showToast({
							title: '加载失败',
							icon: 'success',
							duration: 2000
						})
					};
				});
			}
        }
	}
})
